package com.view.componet;

import java.util.HashSet;
import com.vaadin.data.Item;
import com.vaadin.event.Action;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;


public class TabelaFiltro extends Panel  {

	  HashSet<Object> markedRows = new HashSet<Object>();

	  static final Action ACTION_MARK = new Action("Mark");
	  static final Action ACTION_UNMARK = new Action("Unmark");
	  static final Action ACTION_LOG = new Action("Save");
	  static final Action[] ACTIONS_UNMARKED = new Action[] { ACTION_MARK,
	            ACTION_LOG };
	  static final Action[] ACTIONS_MARKED = new Action[] { ACTION_UNMARK,
	            ACTION_LOG };

	    
	private VerticalLayout mainLayout;
	public  Table tableMain;
	public  TextField filterField;
 	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	
	public TabelaFiltro(String nomeCaption) {
		
	    setCaption(nomeCaption);
		setWidth("100.0%");
		setHeight("100.0%");
	    addComponent(buildMainLayout());
	}

	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		
		
		filterField = new TextField();
		filterField.setCaption("Filtro");
	    filterField.setTextChangeEventMode(TextChangeEventMode.LAZY);
	    filterField.setTextChangeTimeout(200);
	    filterField.setWidth("200px");
	    filterField.setHeight("-1px");
		mainLayout.addComponent(filterField);
		mainLayout.setExpandRatio(filterField, 1.0f);
		mainLayout.setComponentAlignment(filterField, new Alignment(34));
		
		// tableMain
		tableMain = new Table();
		tableMain.setWidth("100.0%");
		tableMain.setHeight("100.0%");
		tableMain.setSelectable(true);
	    tableMain.setColumnReorderingAllowed(true);
	    tableMain.setColumnCollapsingAllowed(true);

		
		mainLayout.addComponent(tableMain);
        
	    tableMain.addActionHandler(new Action.Handler() {
            public Action[] getActions(Object target, Object sender) {
                
                if (markedRows.contains(target)) {
                    return ACTIONS_MARKED;
                } else {
                    return ACTIONS_UNMARKED;
                }
            }

            public void handleAction(Action action, Object sender, Object target) {
                
            	if (ACTION_MARK == action) {
                    markedRows.add(target);
          
                 } else if (ACTION_UNMARK == action) {
                    markedRows.remove(target);
                } else if (ACTION_LOG == action) {
                    Item item = tableMain.getItem(target);
                   
                 }
            }

        });
	    
	     
		return mainLayout;

	
	}
}
